// Copyright (C) ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
//
// $Id: safe_auto_ptr.hpp 318 2009-03-02 14:03:21Z heavyzheng $
//

#ifndef ZZHENG_UTILITY_SAFE_AUTO_PTR_HPP_20080703__
#define ZZHENG_UTILITY_SAFE_AUTO_PTR_HPP_20080703__

#include <zzheng/config.hpp>
#include <boost/noncopyable.hpp>
#include <memory>

namespace zzheng {

    //! \addtogroup utility zzheng.utility
    //! \{

    //! This class template is a modified version of std::auto_ptr. Changes are:
    //! - copy construction and copy assignment are forbidden.
    //! - support deleter function object.
    template<typename T> class safe_auto_ptr: boost::noncopyable {

    public:

        //! Constructs an object to manage a raw pointer.
        explicit safe_auto_ptr(T* raw = 0): ptr_(raw) {
            // Do nothing.
        }

        /*
        //! Constructs an object to manage a raw pointer.
        //! \param raw      the raw pointer to wrap.
        //! \param deleter  the deleter function object to delete the raw pointer.
        template<typename D>
        explicit safe_auto_ptr(T* raw, const D& deleter): ptr_(raw, deleter) {
            // Do nothing.
        }
        */

        //! Dereference operator.
        T& operator*() const {
            return (*ptr_);
        }

        T* operator->() const {
            return ptr_.get();
        }

        //! Returns the wrapped raw pointer but keeps the ownership.
        T* get() const {
            return ptr_.get();
        }

        //! Resets the wrapped raw pointer. If the original wrapped raw pointer is not null, it
        //! will be deleted.
        void reset(T* raw = 0) {
            ptr_.reset(raw);
        }

        //! Releases the ownership of the wrapped raw pointer.
        T* release() {
            return ptr_.release();
        }

    private:

        std::auto_ptr<T> ptr_;

    }; // class safe_auto_ptr

    //! \} end group utility

} // namespace zzheng

#endif // ZZHENG_UTILITY_SAFE_AUTO_PTR_HPP_20080703__



